package sanphone.com.runningschool.Fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;

import sanphone.com.runningschool.ComplaintActivity;
import sanphone.com.runningschool.ForceOffline;
import sanphone.com.runningschool.LogIn.LogInActivity;
import sanphone.com.runningschool.MyMessage.MyMessageActivity;
import sanphone.com.runningschool.MyOrder.MyOrderActivity;
import sanphone.com.runningschool.PersonalInfo.PersonalInfoActivity;
import sanphone.com.runningschool.R;

import static android.content.Context.MODE_PRIVATE;

public class SettingFragment extends Fragment {
    SharedPreferences sprfLogIn;
    SharedPreferences.Editor editorLogIn, editorMain;
    private Button quit;
    private SharedPreferences sprf;
    private TextView nameText;
    private String id,nickName;
    private LinearLayout personalInfo,my_message, my_order, update,Complaint;
    private ForceOffline offline;

    public static SettingFragment newInstance() {
        SettingFragment fragment = new SettingFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.setting_fragment, null);
        sprf = getActivity().getSharedPreferences("data", MODE_PRIVATE);
        id = sprf.getString("schoolId", null);
        nickName=sprf.getString("nickName",null);
        offline = new ForceOffline(id, getActivity());
//        offline.check();
        initView(view);
        click();
        return view;
    }

    private void initView(View view) {

        nameText=view.findViewById(R.id.name_text);
        quit = view.findViewById(R.id.quit);
        personalInfo =  view.findViewById(R.id.personalInfo);
        my_message = view.findViewById(R.id.my_message);
        my_order =  view.findViewById(R.id.my_order);
        update =  view.findViewById(R.id.update);
        Complaint = view.findViewById(R.id.Complaint);
        nameText.setText(nickName);
    }

    public void click() {
       personalInfo.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               startActivity(new Intent(getActivity(),PersonalInfoActivity.class));
           }
       });
       my_message.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               startActivity(new Intent(getActivity(),MyMessageActivity.class));
           }
       });
       my_order.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               startActivity(new Intent(getActivity(),MyOrderActivity.class));
           }
       });
        Complaint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ComplaintActivity.class));
            }
        });
        quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupMenu popupMenu = new PopupMenu(view.getContext(), view);
                MenuInflater inflater = popupMenu.getMenuInflater();
                //填充菜单
                inflater.inflate(R.menu.quit_menu, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        switch (menuItem.getItemId()) {
                            case R.id.exit:
                                System.exit(0);
                                break;
                            case R.id.quit_to_login:
                                resetSprfMain();
                                Intent intent = new Intent(getContext(), LogInActivity.class);
                                startActivity(intent);
                                getActivity().finish();
                                break;
                            default:
                                break;
                        }
                        return true;
                    }
                });

                popupMenu.show();

            }
        });
    }

    public void resetSprfMain() {
        sprfLogIn = PreferenceManager.getDefaultSharedPreferences(getActivity());
        editorLogIn = sprfLogIn.edit();
        editorLogIn.putBoolean("main", false);
        editorLogIn.commit();
        editorMain = getActivity().getSharedPreferences("data", MODE_PRIVATE).edit();
        editorMain.putString("schoolId", null);
        editorMain.putString("userName", null);
        editorMain.putString("userPhone", null);
        editorMain.putString("nickName", null);
        editorMain.putString("dormitory", null);
        editorMain.putString("token",null);
        editorMain.apply();
    }

    @Override
    public void onResume() {
        super.onResume();
        offline.check();
    }
}
